#Model
fml_initial <- ~ initial_report + age + ctr_gender + ctr_education +
  ctr_income + ctr_unemployed+ ctr_ethnicity + ctr_disability+
  daily_new_confirmed_per_million

run_lm_initial <- function(dv, pid, data, bound = NULL) {
  d <- data |>
    filter(date_diff_initial<= 3,date_diff_initial>=-4, partyId == pid)
  
  if (!is.null(bound) && !is.na(bound)) {
    d <- d |> filter(.data[[dv]] <= bound)
  }
  m <- lm(reformulate(attr(terms(fml_initial), "term.labels"), dv), data = d)
  c(
    est = coef(summary(m))[2,1],
    std = coef(summary(m))[2,2]
  )
}


specs <- tribble(
  ~dv, ~bound,
  "likeCon", 10,
  "likeJohnson", 10,
  "handleCorona", 10,
  "govtHandlelockdown", 10,
  "convote", 1,
  "labvote", 1
)

parties <- c(Cons = 1, Lab = 2, Ind = 10)

#Labels
dv_labels <- c(
  likeCon = "Like Cons (0-10)",
  likeJohnson = "Like Johnson (0-10)",
  handleCorona = "COVID-19 Performance (1-5)",
  govtHandlelockdown = "Lockdown Performance (1-5)",
  convote = "Vote Intention: Con (0-1)",
  labvote = "Vote Intention: Lab (0-1)"
)

# Run linear models for each outcome–party specification,
# store estimation results in a tidy data frame,
# and compute 95% and 90% confidence intervals
df_scandal <- expand_grid(specs, party = names(parties)) |>
  mutate(est = NA_real_, std = NA_real_)

for (i in seq_len(nrow(df_scandal))) {
  
  dv_i    <- df_scandal$dv[i]
  pid_i   <- parties[df_scandal$party[i]]
  bound_i <- df_scandal$bound[i]
  
  res <- run_lm_initial(
    dv = dv_i,
    pid = pid_i,
    data = bes_w22_covid,
    bound = bound_i
  )
  
  df_scandal$est[i] <- res["est"]
  df_scandal$std[i] <- res["std"]
}

df_scandal <- df_scandal |>
  mutate(
    Party = factor(party, levels = c("Cons","Lab","Ind")),
    DV = factor(dv, levels = names(dv_labels), labels = dv_labels),
    ul   = est + 1.96 * std,
    ll   = est - 1.96 * std,
    ul90 = est + 1.645 * std,
    ll90 = est - 1.645 * std
  )
    
#Function for Figure 2
plot_block <- function(df) {
  ggplot(df) +
    geom_linerange(aes(x = Party, ymin = ll90, ymax = ul90),
                   linewidth = 2, color = "black") +
    geom_pointrange(aes(x = Party, y = est, ymin = ll, ymax = ul),
                    size = 2, linewidth = 1, color = "black") +
    facet_wrap(~ DV) +
    geom_hline(yintercept = 0, linetype = 2) +
    labs(y = "Effect of Initial Report") +
    theme_light() +
    theme(
      text = element_text(size = 30),
      strip.text = element_text(size = 26, color = "black"),
      strip.background = element_rect(fill = "grey90"),
      panel.grid = element_blank()
    )
}

#Figure 2
initial_covid <- plot_block(
  df_scandal |>
    filter(dv %in% c("handleCorona", "govtHandlelockdown"))
)
initial_covid

initial_like <- plot_block(
  df_scandal |>
    filter(dv %in% c("likeCon", "likeJohnson"))
)
initial_like


initial_vote <- plot_block(
  df_scandal |>
    filter(dv %in% c("convote", "labvote"))
)
initial_vote

ggsave("initial_covid.eps",gg_initial_covid,width = 14,height = 6,dpi = 1200)
ggsave("initial_like.eps",gg_initial_like,width = 14,height = 6,dpi = 1200)
ggsave("initial_vote.eps",initial_vote,width = 14,height = 6,dpi = 1200)


